package com.eibus.applicationconnector.dotnet;

import junit.framework.TestCase;

import com.eibus.connector.nom.Connector;
import com.eibus.xml.nom.Node;

//@Author	: Lejil
//@Class	: DotnetConnectorErrorTest


/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	Invoke the .NET public Instance & Static Methods
 *				For all the dataTypes with Incorrect Values
 */

public class DotnetConnectorErrorTestCase extends TestCase
{

	private Connector  connector ;
	private final String faultCode	=	"Arguments.Type-Mismatch";


	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();

		}
		catch(Exception exception)
		{
			fail("Could not get instance of Connector. "+exception.getMessage());
		}
	}

	protected void tearDown()
	{
	}

	public DotnetConnectorErrorTestCase(String name)
	{
		super(name);
	}


	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(DotnetConnectorErrorTestCase.class);
	}



	public int  sendRequest(String methodName,String argsOne,String argsTwo)
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod("http://schemas.cordys.com/1.0/dotnet/UnitTestMethods",methodName);
			Node.createTextElement("args1",argsOne,request);
			Node.createTextElement("args2",argsTwo,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- " + methodName +"  " + ex.toString());
		}
		finally
		{
			Util.delete(Node.getRoot(request));
		}
		return response;
	}




	public  boolean checkforSoapFault(int response) throws Exception
	{
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);

			if ( faultNode == 0)
			{
				fail(".NET Connector Expected soapFault, but returned Incorrect result" );
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}
    
    private void commonTestMethod(String methodName, String argsOne, String argsTwo)
    {
       commonTestMethod(methodName, argsOne, argsTwo, faultCode);
    }
    
    private void commonTestMethod(String methodName, String argsOne, String argsTwo, String expectedFaultcode)
    {
        int response =0;
        try
        {
             response =sendRequest(methodName, argsOne, argsTwo);
             //String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
             String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
             if (!result.equals(expectedFaultcode))
             {
                 //fail(methodName +  " Expected value is "+ expectedFaultcode+", but returned " + result);
                 System.out.println(methodName);
                 System.out.println("Expected value is : " + expectedFaultcode);
                 System.out.println("Result value is : " + result);
                 System.out.println("faultstring is : " + Node.getDataWithDefault(XPathUtil.getSOAPFaultstring(response), "No SOAP Fault"));
             }
             //assertEquals(expectedFaultcode, result);
        }
        catch(Exception ex)
        {
            fail(".NET Connector junit Failed for the .NET Method :- " + methodName + " :: "+ex.toString());
        }
        finally
        {
            Util.delete(Node.getRoot(response));
        }
    }


	public void testBooleanType() //1
	{
        commonTestMethod("TestBoolType","q2o1","false");
	}


	public void testByteType() //2
	{
        commonTestMethod("TestByteType","a1","2901");
	}


	public void testCharType() //3
	{
        commonTestMethod("TestCharType","india","hyderabad");
	}

	public void testDoubleType() //4
	{
        commonTestMethod("TestDoubleType","Cordys4.2334","24440.23Hyderabad");
	}

	public void testIntegerType() //5
	{
        commonTestMethod("TestIntegerType","999ae999","28.88");
	}

	public void testLongType() //6
	{
        commonTestMethod("TestLongType","12345aab,k7890","12.23.344");
	}
    
	public void testShortType() //7
	{
        commonTestMethod("TestShortType","1248*8","12888");
	}
    
	public void testSingleType() //8
	{
        commonTestMethod("TestSingleType","122fg.52","23.52");
	}

	public void testXMLType() //10
	{
        commonTestMethod("TestXmlType","<a>Cordys India Pvt Ltd,Hyderabad<a>","<a>India</a>", "Server.NotImplemented");
	}

	public void testXMLNomType() //11
	{
        commonTestMethod("TestXMLNomType","uio201","243");
	}

	public void testDateTimeType() //12
	{
        commonTestMethod("TestDateTimeType","Hyderabad","India");
	}


	//
	//		STATIC METHODS
	//

	public void testStaticBooleanType() //1
	{
        commonTestMethod("TestStaticBoolType","1rde","yalse");
	}


	public void testStaticByteType() //2
	{
        commonTestMethod("TestStaticByteType","a0","561");
	}


	public void testStaticCharType() //3
	{
        commonTestMethod("TestStaticCharType","abcd","cdef");
	}

	public void testStaticDoubleType() //4
	{
        commonTestMethod("TestStaticDoubleType","223dd324.3335512334","2445df40.233343");
	}

	public void testStaticIntegerType() //5
	{
        commonTestMethod("TestIntegerType","99ghad999","288dd8");
	}


	public void testStaticLongType() //6
	{
        commonTestMethod("TestStaticLongType","12345qwert4567890","123asdf44");
	}
	public void testStaticShortType() //7
	{
        commonTestMethod("TestStaticShortType","12ad45","124er5");
	}


	public void testStaticSingleType() //8
	{
        commonTestMethod("TestStaticSingleType","12.5r2","23.rt52");
	}

	public void testStaticXMLType() //10
	{
        commonTestMethod("TestStaticXmlType","12","<a>India</a>", "Server.NotImplemented");
	}

	public void testStaticXMLNomType() //11
	{
        commonTestMethod("TestStaticXMLNomType","2*aw","India");
	}

	public void testStaticDateTimeType() //12
	{
        commonTestMethod("TestStaticDateTimeType","Cordys India Pvt Ltd,Hyderabad","India");
	}
}